<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@page import="java.util.*" %>
<%@page import="java.text.SimpleDateFormat" %> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>
<LINK REL=StyleSheet HREF="dimming.css" TYPE="text/css">
<SCRIPT LANGUAGE="JavaScript" SRC="dimmingdivsales.js">
</SCRIPT>

<style>
div.dimmer {
	visibility: hidden;
	position:absolute;
	left:0px;
	top:0px;
	font-family:verdana;
	font-weight:bold;
	padding:40px;

	background-image:url(honey.png);
	/* ieWin only stuff */
	_background-image:none;
	_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='honey.png');
}

div.dimming {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-style: normal;
	background-color: #ffffff;
	position:absolute;
	/* set z-index higher than possible */
	z-index:10000;
	visibility: hidden;
	
	border-style: solid;
	border-color: #999999;
	border-width: 1px;

}

table.floatingHeader {
	background-color: #0000ff;
	color:#ffffff;
}
</style>

<script type="text/javascript">
	
	$(document).ready(function() 
	{
			$('#enter1').click(function(e) 
	    	{
   				 $('.f1').undim()
	    	});
	});
	
</script>

<%-- Save customer master details using ajax --%>
<%--
<script  type="text/javascript">

	$(document).ready(function() 
	{
    	$('#enter1').click(function(e) 
    	{
    		alert("In Ajax..");
       		e.preventDefault();
 
       		var ajaxdata = $("#customername").val();
       		var ajaxdata1 = $("#customercode").val();
       		var ajaxdata2 = $("#customeraddress").val();
 			var ajaxdata3 = $("#customercity").val();
  			var ajaxdata4 = $("#customerstate").val();
		    var ajaxdata5 = $("#customertin").val();
		    var ajaxdata6 = $("#customerpan").val();
		    var ajaxdata7 = $("#customermobile").val();
		    var ajaxdata8 = $("#customeremail").val();
		    var ajaxdata9 = $("#customercontactperson").val();
		    var ajaxdata10 = $("#copeningbalance").val();
  
   			var value ='customername='+ ajaxdata +'&customercode='+ ajaxdata1 +'&customeraddress='+ ajaxdata2 +'&customercity='+ ajaxdata3 +'&customerstate='+ ajaxdata4 +'&customertin='+ ajaxdata5 +'&customerpan='+ ajaxdata6 +'&customermobile='+ ajaxdata7 +'&customeremail='+ ajaxdata8 +'&customercontactperson='+ ajaxdata9 +'&copeningbalance='+ ajaxdata10 ;
   			
   			var c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11;
   		  	var namevalid=/^[a-zA-Z ]+$/;
   		  
   	        c1= document.f1.customername.value;

   	        if(c1.length==0)
   	        {
   	        	alert("Enter Customer Name");
   	            return false;
   	      	}
   	        else if(c1.search(namevalid)==-1)
   	        {
   	            alert("Please Enter valid name");
   	            document.f1.customername.value="";
   	            document.f1.customername.focus();
   	            
   		         return false;
   	    	 }
   	         
   	         c3=document.f1.customeraddress.value ;
   	         if(c3.length==0)
   	         {
   	              alert("Please Enter Address");
   	              return false;
   	          }

   		      c4= document.f1.customercity.value;
   	          if(c4.length == 0)
   	          {
   	              alert("Please Enter City");
   	              return false;
   	          }

   	         c5=document.f1.customerstate.value;

   	         if(c5.length == 0)
   	         {
   	              alert("Please Enter State");
   	              return false;
   	         }

   	         c6= document.f1.customertin.value;
   	         if(c6.length == 0)
   	         {
   	              alert("Please Enter TIN Number");
   	              return false;
   	         }

   	         c7= document.f1.customerpan.value;
   	         if(c7.length == 0)
   	         {
   	              alert("Please Enter PAN Number");
   	              return false;
   	         }
   	         
   	         var PAN_REGEX= "^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$";
   	         
   	         if(!c7.match(PAN_REGEX))
   	         {
   	        	 alert("Please Enter PAN Number in right format. For ex- AAAAA1111A")
   	        	 return false;
   	         }

   	         c8= document.f1.customermobile.value;
   	         if(c8.length == 0)
   	         {
   	             alert("Please Enter Mobile Number");
   	             return false;
   	         }
   	         
   	         if(c8.indexOf(" ")!=-1)
   	    	 {
   	       		alert("Invalid Mobile No.");
   	      		return false;
   	    	 }
   	    	 else if(c8.length>10)
   	     	 {
   	         	alert("Mobile Number should be 10 digit");
   	       		return false;
   	     	 }

   	  		else  if (c8.charAt(0)=="0")
   			{
   	  	 		alert("Mobile Number should not start with 0");
   	 			//  document.getElementById('empcontno').focus();
   	 			 return false;
   			}
   	  		else if(!(c8.length==10))
   	     	{
   	        	 alert("Mobile No. should be 10 digit");
   	        	// document.getElementById('empcontno').focus();
   	        	 return false;
   	     	}

 	         
   	         c9= document.f1.customeremail.value;
   	         
   	         var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	         
   	         if(regex.test(c9) == false)
   	         {
   	              alert("Invalid Email Address");
   	              return false;
   	          }
   	   
   	         c10= document.f1.customercontactperson.value;
   	         if(c10.length==0)
   	         {
   	         	alert("Enter Contact Person Name");
   	             return false;
   	       	 }
   	         else if(c10.search(namevalid)==-1)
   	         {
   	             alert("Please Enter valid name");
   	             document.f1.customercontactperson.value="";
   	             document.f1.customercontactperson.focus();
   	             
   	 	         return false;
   	     	 }
   	         
   	         c11= document.f1.copeningbalance.value;
   	         if(c11.length==0)
   	         {
   	         	alert("Enter Opening Balance");
   	             return false;
   	       	 }
   	         
   	         if(!c11.match(/^\d+$/))
   	       	 {
   	        	 alert("Please Enter Opening Balance in Numerics only");
   	             return false;
   	       	 }

   
		$.ajax({
   		     type:"get",  
   		     url: "CustomerMasterServlet", 
	         data: value,
			 cache: false,
			 success: function(data) 
			 {

				 	alery("success");
				 
				 $('.success').fadeIn(200).show();
				 $('.error').fadeOut(200).hide();
   			  }
			                                                               
   			});
       
  		 return false;
		});
   }); 	
</script>
--%>

<%-- customer master window will be open using dimmer view --%>
<script language="javascript">

	function displayWindow()
	{
	      var w, h, l, t;
	       w = 400;
	       h = 400;
	       l = screen.width/4;
	       t = screen.height/4;
                
           // with title		        
		   displayFloatingDiv('windowcontent', 'Dimming Div', w, h, l, t);
	 }
</script>
 
<script language="javascript" type="text/javascript"> 

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in YYYY/MM/DD Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}
</script>

<%-- functions to calculate amounts in sales voucher --%>

<script language="javascript" type="text/javascript"> 

function populateCustomerId(){
    var custname = document.getElementById('custname');

    /* selected value of dropdown */
    var selectedCustomerId = custname.options[custname.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('custcode').value = selectedCustomerId; 
}
     
</script>

<script language="javascript" type="text/javascript">

function NetAmount()
{
    var p1 = document.getElementById("prodquantity").value;
    var p2 = document.getElementById("prodrate").value;
   
    var p3=parseInt(p1) * parseFloat(p2);
 
    document.getElementById("netamount").value = p3; 
}
    
</script>   

<script language="javascript" type="text/javascript">

function TaxAmount(opt)
{
	var tax1 = document.getElementById("netamount").value;
	var tax2= opt.text;
	
	var nettax= (tax1 * tax2) /100;
	document.getElementById("taxamount").value = nettax;	
}
</script>

<script language="javascript" type="text/javascript">

function TotalAmount()
{
	var t1 = document.getElementById("netamount").value;
	var t2 = document.getElementById("taxamount").value;
	var t3 = document.getElementById("transport").value;
	
	var total=parseFloat(t1)+parseFloat(t2)+parseFloat(t3);
	
	document.getElementById("totalamount").value = total;
}
	
</script>

<%-- to refresh customer list in sales voucher --%>
<script>
	
	$(function() 
	{
  		$("#enter1").on("click", function() {
     $("#refreshcustomer").load("SalesVoucher2.jsp")
     return false;
 })
})
</script>

<title>Sales Voucher</title>
</head>
<body>

<%--sales voucher form --%>

<form name="f11"  action=SalesVoucherServlet ONSUBMIT="return checkDate()"> 

<input type="hidden" name="jspname" value="SalesVoucher">

<table cellspacing=3>
<tr>
	<td> Against Sales order number </td>
	<td> <input type="text" name="ordnum" /> </td>
<tr>
	<td>Invoice number</td>
	
			<%	
					DBConnection dbc=new DBConnection();	
				 	Connection con=dbc.getNewConnection();
				 	String code2="C1";
		
					Statement st = null;
					ResultSet rs = null;

			 	String code3="SV1";
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select MAX(innum) from SalesVoucher2"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;
	   		       
            	}  %>
  
	 		<% }
		   catch(Exception e){} %>
		   
	<td><input type="text" name="Innum" value="<%=code3 %>"/></td>
	
	<td>Date</td>

	<% SimpleDateFormat current = new SimpleDateFormat("yyyy/MM/dd"); %>	
	<td><input type="text" name="dte" value="<%=current.format(new Date()) %>" /></td>
	
</tr>

<tr>
	<td>Name of Customer</td>
	<!--  <td><input type="text" name="custname"/></td> -->
	

			<%	
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
			
			<td> 
	  			<select name="custname" id="custname" onchange="populateCustomerId();">
	  				<option> </option>
       		 		<%  while(rs.next()){ %>
            			<option value="<%=rs.getString(1)%>" ><%= rs.getString(2)%></option>
      
     			    <% } %>
      			</select>
    		</td>
  
			<% }
			catch(Exception e){} %>
	

		<%--<td> <input type="button" name="custbutton" value="Create customer" onclick="popup()"></td>  --%>
		
		 <td> <a href="javascript:displayWindow();"> Create Customer </a> 
	
</tr>

<tr>
	<td>Customer code</td>
	<td><input type="text" name="custcode" id="custcode" />
	<td>Customer Invoice number</td>
	<td><input type="text" name="custinnum" /> </td>
	</tr>
	
<tr>
	<td>Product Name</td>
	<!-- <td><select name="prodname"></select></td> -->
	
		 <%	 
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select stname from StockMaster"); %>
	<td> 
	  <select name="prodname">
	  	<option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){} %>
	
	<td><a href="javascript:displayWindow();"> Create New Stock </a></td>
	<td><input type="submit" name="act" value="Refresh Item names"/></td>
	</tr>
	
<tr>
	<td>Product Quantity</td>
	<td><input type="text" name="prodquantity" id="prodquantity"  /></td>
	
	<td>Product Rate</td>
	<td><input type="text" name="prodrate" id="prodrate" /></td>
</tr>

<tr> 
	<td>Net Amount</td>
	<td><input type="text" name="netamount" id="netamount" /></td>
	
	<td><input type="button" value="Calculate Net Amount"  onClick="NetAmount();"/></td>
	
</tr>
<tr>
	<td>Taxation Category</td>
	<td>
	<select name="taxcat" id="taxcat" onchange="TaxAmount((this.options[this.selectedIndex]));" >
	<option> </option>
	<option value="5.0"> 5.0 </option>
	<option value="12.5"> 12.5 </option>
	</select>
	</td>
</tr>
	
<tr>
	<td>Tax Amount</td>
	<td><input type="text" name="taxamount" id="taxamount" /></td>
	<td><input type="button" name="act" value="Calculate Tax Amount" /></td>
</tr>
	
<tr>
	<td>Transport Charges</td>
	<td> <input type="text" name="transport" id="transport" /></td>
</tr>
	
<tr>
	<td>Total Amount</td>
	<td><input type="text" name="totalamount" id="totalamount" /></td>
	<td><input type="button" name="act" value="Calculate Total Amount" onClick="TotalAmount();" /></td>
</tr>
	
<tr>
	<td>Credit limit (No. of Days)</td>
	<td><input type="text" name="credlimit"/></td>
	</tr>
<tr>
	<td><input type="submit" name="enter" value="Enter" /></td>
</tr>
</table>
</form>


<%-- Customer Master form --%>

<div style="width: 518px; height: 287px; visibility:hidden"; id="windowcontent">

<form  name="f1" action="CustomerMasterServlet" method=get ONSUBMIT="return check()" enctype="multipart/form-data"> 

<input type="hidden" name="jspname" value="SalesVoucher2.jsp">
<table>
	
<tr>
	<th>	</th>
	<th>	</th>
</tr>

<tr>
	<td> Customer Name </td>
	<td> <input type="text" name="customername" id="customername"> </td>
</tr>

<tr>
	<td> Customer Code </td>
	
		<%	
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode from CustomerMaster"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;

            	} %>
  
	 		<% }
		   catch(Exception e){} %>
		
	<td> <input type="text" id="customercode" name="customercode" value="<%=code2 %>" > </td>
</tr>

<tr>
	<td> Address</td>
	<td> <input type="text" id="customeraddress" name="customeraddress"> </td>
</tr>

<tr>
	<td> City </td>
	<td> <input type="text" id="customercity" name="customercity"> </td>
</tr>

<tr>
	<td> State </td>
	<td> <input type="text" id="customerstate" name="customerstate"> </td>
</tr>

<tr>
	<td> TIN Number </td>
	<td> <input type="text" id="customertin" name="customertin"> </td>
</tr>

<tr>
	<td> PAN Number</td>
	<td> <input type="text" id="customerpan" name="customerpan"> </td>
</tr>

<tr>
	<td> Mobile Number</td>
	<td> <input type="text" id="customermobile" name="customermobile"> </td>
</tr>

<tr>
	<td> Email ID </td>
	<td> <input type="text" id="customeremail" name="customeremail"> </td>
</tr>

<tr>
	<td> Contact Person Name </td>
	<td> <input type="text" id="customercontactperson" name="customercontactperson"> </td>
</tr>

<tr>
	<td> Opening Balance </td>
	<td> <input type="text" id="copeningbalance" name="copeningbalance"> </td>
</tr>

<tr>
	
	<td> <input type="button" id="enter1" name="enter1" value="Submit"> </td>
</tr>

</table>

</form>
</div>


</body>
</html>